package com.zmx.common.common.repository;

import com.zmx.common.common.entitys.BaseEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;

import java.util.Date;
import java.util.List;

/**
 * @Description: Repository通用接口
 * @ClassName: BaseRepository
 * @Author zhaomxr
 * @Date 2021-09-29 09:23
 */
@NoRepositoryBean
public interface BaseRepository<T extends BaseEntity> extends BaseAnyRepository<T> {

    @Query("select t from #{#entityName} t where t.dr = 0 and t.id in ?1")
    List<T> findByIdIn(Long[] ids);

    @Query("select t from #{#entityName} t where t.dr = 0 and t.id = ?1")
    T findById(Long id);

    /**
     * 通过id和ts查询小于当前时间数据，主要用于并发控制中 版本检查
     * @param id
     * @param ts
     * @return
     */
    @Query("select count(1) from #{#entityName} t where t.dr = 0 and t.id = ?1 and t.ts <= ?2")
    int countByIdAndTs(Long id, Date ts);
}